﻿@{
    var action = UrlData[0].ToLower();
    string path = "~/Content/Templates/cif_template.xls";
    string delTempPath = "~/Content/Templates/card_delivery_template.xls";
    string ccappnos = Request["ccappnos"];
    string pbank = UtilityManager.GetVal(Request["pbank"]);
    
    var ccList= CashCardManager.ExtractSelected(ccappnos);
   
    byte[] byteArray=null;
    //write a file in folder
    DateTime today = DateTime.Today;
    string ctrPath=string.Empty;
    string ctr = string.Empty;
    string fileName=string.Empty;
    string folder=string.Empty;
    
    if(action.Equals("printing")){
        // byteArray= UtilityManager.GenerateXLFromTemplate(Server.MapPath(path),"Fns_Conv",ccList);
         ctrPath=Server.MapPath("~/App_Data/print_filectr.txt");
         ctr = UtilityManager.GetCounterForFileExt(ctrPath);
    
         byteArray= UtilityManager.GenerateXLFromTemplate2(Server.MapPath(path),"Fns_Conv",ccList);
         if(pbank!="0"){
           fileName = string.Format("CIF_{0}_{1}_{2}.xls",pbank,today.ToString("MM-dd-yyy"),ctr);
         }else{
           fileName = string.Format("CIF_{0}_{1}.xls",today.ToString("MM-dd-yyy"),ctr);
         } 
         folder="~/Content/Printing/";
    }
    if(action.Equals("activation")){
         ctrPath=Server.MapPath("~/App_Data/activation_filectr.txt");
         ctr = UtilityManager.GetCounterForFileExt(ctrPath);
    
         byteArray= UtilityManager.GenerateXLFromTemplate2(Server.MapPath(path),"Fns_Conv",ccList);
        
         if(pbank!="0"){
           fileName = string.Format("ReqAct_{0}_{1}_{2}.xls",pbank,today.ToString("MM-dd-yyy"),ctr);
         }else{
           fileName = string.Format("ReqAct_{0}_{1}.xls",today.ToString("MM-dd-yyy"),ctr);
      
         }
         folder="~/Content/Activation/";
    }
    
    if(action.Equals("delivery")){
         ctrPath=Server.MapPath("~/App_Data/delivery_filectr.txt");
         ctr = UtilityManager.GetCounterForFileExt(ctrPath);
    
         byteArray= UtilityManager.GenerateXLForDelivery(Server.MapPath(delTempPath),"Delivery",ccList);
         fileName = string.Format("DELIVERY_{0}_{1}.xls",today.ToString("MM-dd-yyy"),ctr);
         folder="~/Content/Delivery/";
    }
    
    try{
        File.WriteAllBytes(Server.MapPath(folder+fileName),byteArray);
         //update status of cashcards
        CashCardManager.UpdateDetailsForExtCC(ccappnos,fileName,action,@WebSecurity.CurrentUserName);
    }catch(Exception e){
        throw e;
    }
    
    AuditManager.Log("generateExcel","generateExcel",fileName,"",@WebSecurity.CurrentUserName,"","");
    //clear all content output from the buffer stream
    
    Response.Clear();
    //add HTTP Header to the output stream and speficy default name for the browser download dialog
    
    Response.AddHeader("Content-Disposition","attachment; filename="+fileName);
    //add header for file size
    Response.AddHeader("Content-Length",byteArray.Length.ToString());
    //set HTTP Mime Type
    Response.ContentType="application/vnd.ms-excel";
    //Write data to the client
    Response.BinaryWrite(byteArray);
    Response.Flush();
    Response.End();
    
}